<?php

namespace app\api\controller;

use think\Controller;

class Imei extends Controller 
{
    
    public function add()
    {
        $get = request()->get();
        if(empty($get['user_id'])){
            mylog('user_id为空');
            return 'user_id为空';
        }
        if(empty($get['imei']) && empty($get['oaid'])){
            mylog('imei,oaid全部为空');
            return 'imei,oaid全部为空'; 
        }

        $reg = db('imei_user', 'db_operatelog')->where('user_id', '=', $get['user_id'])->find();

        if (!empty($reg)) {
            mylog('user_id exists');
            return 'user_id exists';
        }

        $reg = db('imei_user', 'db_operatelog')->insert(
            [
                'user_id' => $get['user_id'],
                'imei' => isset($get['imei'])?$get['imei']:'',
                'oaid' => isset($get['oaid'])?$get['oaid']:'',
                'add_time' => date('Y-m-d H:i:s'),
            ]
        );

        return 'ok';
    }

    public function getUserInfo(){
        $get = request()->get();
        if(empty($get['imei'])){
            mylog('imei为空');
            return 'imei为空';
        }
        $reg = db('imei_user', 'db_operatelog')->where('imei|oaid', '=', $get['imei'])->find();
        if (empty($reg)) {
            mylog('not exists');
            return 'not exists';
        }

        
        $account = db('accounts_info','db_config2')->field('Nickname as nickname,TotalRecharge as paymount,TotalLottery as ticketNum')
        ->where('UserID','=',$reg['user_id'])
        ->find();
        
        if(!$account){
            mylog('accounts_info not exists');
            return 'accounts_info not found';
        }
        
        $account['uid'] = $reg['user_id'];
        $account['imei'] = $reg['imei'];
        $account['oaid'] = $reg['oaid'];

        $phone = db('user_bind_info','db_config2')->where('UserID','=',$reg['user_id'])->value('Phone');
        if($phone){
            $account['phone'] = $phone;
        }

        return json_encode($account); 
    }
}
